﻿--[[
   @brief    tray_manager control
   @author   rickonzhang
   @version  1.0
   @date     2015/11/12
--]]

tray_manager_control = 
{
    FORBID_MSG = 2;

    init = function()
        tray_manager_control.log("init");
        ierd_tgp.tpf_ui.Require("tray_manager\\model.lua");
    end;

    log = function(str)
        base_utility.log("[tray_manager_control]"..tostring(str));
    end;

    switch_status = function(status)
        tray_manager_control.log("switch_status, status:"..tostring(status));
        tray_manager_model.current_tray_status = status;
    end;

    show_msg_box_notify_reddot = function(event_id, json_str)
        tray_manager_control.log("[show_msg_box__notify_reddot] current_tray_status:"..tray_manager_model.current_tray_status);
        -- 闪烁消息提示
        if tray_manager_model.current_tray_status ~= TRAY_STATUS.TRAY_SYSTEM_NOTIFY then
            tqos_mgr.increase_tray_icon_trig_msg_flash_count();
            base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):show_message_tray_animation();
            tray_manager_control.switch_status(TRAY_STATUS.TRAY_SYSTEM_NOTIFY);
        end
        tray_manager_model.update_tray_status_time("notify");
    end;

    clear_msg_box_notify_reddot = function(event_id, json_str)
        tray_manager_control.log("[clear_msg_box__notify_reddot] current_tray_status:"..tray_manager_model.current_tray_status);
        tray_manager_model.remove_tray_status("notify");
        -- 取消闪烁消息提示
        if tray_manager_model.current_tray_status == TRAY_STATUS.TRAY_SYSTEM_NOTIFY then
            base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):restore_normal_tray_icon();
            tray_manager_control.switch_status(TRAY_STATUS.TRAY_SYSTEM_NORMAL);
            tray_manager_control.show_preview_tray_icon();
        end
    end;

    friend_have_unread_msg = function(event_id, json_str)
        if pwg_social_control:is_new_social_enabled() then
            return;
        end

        if base_utility.is_type_of_oversea_ver() then
            return;
        end

        local param = base_utility.json_to_table(json_str);
        local friend_id = param["friend_id"];

        local friend_info = friend_list_model.get_friend_info(friend_id);
        if friend_info ~= nil then
            if friend_info.last_msg_ == nil then
                base_utility.log("[friend_have_unread_msg] friend:"..friend_id.." havae no last msg!", true);  
                return;
            end
        else
            return;
        end

        tray_manager_model.insert_have_unread_msg_friend_id(friend_id);

        local is_session_opened = talk_dialog_control.is_friend_session_opened(friend_id);
        tray_manager_control.log("[friend_have_unread_msg]friend_id:"..friend_id..", is_session_opened:"..tostring(is_session_opened)..",current_tray_status:"..tray_manager_model.current_tray_status);
        -- 有小红点，且对话session还没打开，且当前没有闪烁聊天信息图标
        if is_session_opened == false then
            if tray_manager_model.current_tray_status ~= TRAY_STATUS.TRAY_TALK_MESSAGE then
                tqos_mgr.increase_tray_icon_trig_talk_flash_count();
                base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):show_talk_tray_animation();
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_TALK_MESSAGE);
            end
            tray_manager_model.update_tray_status_time("talk");
        end

    end;

    pwg_friend_have_unread_msg = function(event_id, json_str)
        local unread_info = comm_center.call_service("i_service_new_social_query_unread_message",
                                                     {});
        if nil == unread_info then return end;
        if nil == unread_info.msgs or type(unread_info.msgs) ~= 'table' then return end;
        local msgs = unread_info.msgs or {};
        for idx, msg in ipairs(msgs) do
            if nil ~= msg.lastest_msg then
                local friend_id = msg.lastest_msg.sender_id;
                if nil ~= friend_id and "" ~= friend_id then
                    tray_manager_model.insert_have_unread_msg_friend_id(friend_id);
                end
            end
        end

        if tray_manager_control.is_all_msg_processed() == false then
            if tray_manager_model.current_tray_status ~= TRAY_STATUS.TRAY_TALK_MESSAGE then
                tqos_mgr.increase_tray_icon_trig_talk_flash_count();
                base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):show_talk_tray_animation();
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_TALK_MESSAGE);
            end
            tray_manager_model.update_tray_status_time("talk");
        end

    end;

    clear_friend_msg = function(event_id, json_str)
        tray_manager_control.log("[clear_friend_msg]");
        local param = base_utility.json_to_table(json_str);
        local friend_id = param["friend_id"];
        tray_manager_model.remove_have_unread_msg_friend_id(friend_id);
        tray_manager_control.log("[clear_friend_msg] friend_id:"..friend_id..", current_tray_status:"..tray_manager_model.current_tray_status);
        if tray_manager_model.current_tray_status == TRAY_STATUS.TRAY_TALK_MESSAGE then
            -- 当前如果在闪烁聊天消息图标， 则需要判断后面还有消息是否未读来决定是否继续闪烁
            if tray_manager_control.is_all_msg_processed() == true then
                base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):restore_normal_tray_icon();
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_SYSTEM_NORMAL);
                tray_manager_model.remove_tray_status("talk");
                tray_manager_control.log("[clear_friend_msg] all processed");
                tray_manager_control.show_preview_tray_icon();
            else
                tray_manager_control.log("[clear_friend_msg] not all processed");
            end
        end
    end;

    group_have_unread_msg = function(event_id, json_str)
        if pwg_social_control:is_new_social_enabled() then
            return;
        end

        if base_utility.is_type_of_oversea_ver() then
            return;
        end

        local param = base_utility.json_to_table(json_str);
        local group_id = param["group_id"];

        -- 设置了屏蔽闪烁提醒的需要忽略掉
        if group_list_model.get_group_mask_msg_flag(group_id) == tray_manager_control.FORBID_MSG then
            base_utility.log("[group_have_unread_msg] group:"..group_id.." forbid message flush", true);
            return;
        end

        local group_info = group_list_model.get_group_info(group_id);
        if group_info == nil or 
           group_info.group_type == nil or
           string.len(group_info.group_type) == 0 or
           group_info.group_type == "MatchLiveRoom" or 
           group_info.group_type == "CommonTopicRoom" then
            return;
        end

        if group_info.last_msg_ == nil then
            base_utility.log("[group_have_unread_msg] group:"..group_id.." havae no last msg!", true);            
            return;
        end

        tray_manager_model.insert_have_unread_msg_group_id(group_id);

        local is_session_opened = talk_dialog_control.is_group_session_opened(group_id);
        tray_manager_control.log("[group_have_unread_msg]group_id:"..group_id..", is_session_opened:"..tostring(is_session_opened)..",current_tray_status:"..tray_manager_model.current_tray_status);

        -- 有小红点，且对话session还没打开，且当前没有闪烁聊天信息图标
        if is_session_opened == false then
            if tray_manager_model.current_tray_status ~= TRAY_STATUS.TRAY_TALK_MESSAGE then
                tqos_mgr.increase_tray_icon_trig_talk_flash_count();
                base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):show_talk_tray_animation();
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_TALK_MESSAGE);
            end
            tray_manager_model.update_tray_status_time("talk");
        end
    end;

    clear_group_msg = function(event_id, json_str)
        tray_manager_control.log("[clear_group_msg]");
        local param = base_utility.json_to_table(json_str);
        local group_id = param["group_id"];
        tray_manager_model.remove_have_unread_msg_group_id(group_id);
        tray_manager_control.log("[clear_group_msg] group_id:"..group_id..", current_tray_status:"..tray_manager_model.current_tray_status);
        if tray_manager_model.current_tray_status == TRAY_STATUS.TRAY_TALK_MESSAGE then
            -- 当前如果在闪烁聊天消息图标， 则需要判断后面还有消息是否未读来决定是否继续闪烁
            if tray_manager_control.is_all_msg_processed() == true then
                base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):restore_normal_tray_icon();
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_SYSTEM_NORMAL);
                tray_manager_model.remove_tray_status("talk");
                tray_manager_control.log("[clear_group_msg] all processed");
                tray_manager_control.show_preview_tray_icon();
            else
                tray_manager_control.log("[clear_group_msg] not all processed");
            end
        end
    end;

    is_all_msg_processed = function()
        local unread_msg_friend_id_list = tray_manager_model.get_unread_msg_friend_id_list();
        if unread_msg_friend_id_list and next(unread_msg_friend_id_list) then
            for index, friend_id in ipairs(unread_msg_friend_id_list) do
                if talk_dialog_control.is_friend_session_opened(friend_id) == false then
                    tray_manager_control.log("[is_all_msg_processed] not friend all processed");
                    return false;
                end
            end
        end

        local unread_msg_group_id_list = tray_manager_model.get_unread_msg_group_id_list();
        if unread_msg_group_id_list and next(unread_msg_group_id_list) then
            for index, group_id in ipairs(unread_msg_group_id_list) do
                if talk_dialog_control.is_group_session_opened(group_id) == false then
                    tray_manager_control.log("[is_all_msg_processed] not group all processed");
                    return false;
                end
            end
        end

        tray_manager_control.log("[is_all_msg_processed] all processed");
        return true;
    end;

    -- 显示前一个icon或者动画
    show_preview_tray_icon = function()
        local status_list = tray_manager_model.get_tray_status_list();
        if status_list and next(status_list) then
            local last_item = status_list[1];
            if last_item["status"] == "predownload" then
                tray_tips_view.show(true);
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_PREDOWNLOAD_ANI);
            elseif last_item["status"] == "talk" then
                base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):show_talk_tray_animation();
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_TALK_MESSAGE);
            elseif last_item["status"] == "notify" then
                base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):show_message_tray_animation();
                tray_manager_control.switch_status(TRAY_STATUS.TRAY_SYSTEM_NOTIFY);
            else
                tray_manager_control.log("[show_preview_tray_icon]error!");
            end
        end
    end;

    on_lbuttonup_on_tray_icon = function(event_id, json_str)
        tray_manager_control.log("[on_lbuttonup_on_tray_icon]");
        if tray_manager_model.current_tray_status == TRAY_STATUS.TRAY_TALK_MESSAGE then
            if right_corner_pop_wnd_view ~= nil then
                right_corner_pop_wnd_view.hide_immediately();
            end
            tqos_mgr.increase_tray_icon_click_open_talk_wnd_count();
            tray_manager_control.open_all_talk_session();
        elseif tray_manager_model.current_tray_status == TRAY_STATUS.TRAY_SYSTEM_NOTIFY then
            msg_box_frame_control.show_report_count_flag(0);
            if right_corner_pop_wnd_view ~= nil then
                right_corner_pop_wnd_view.hide_immediately();
            end
            tqos_mgr.increase_tray_icon_click_open_msg_wnd_count();
            -- message_box_control.show(nil, nil);
            event_center.send_event(group_event.SHOW_MESSAGE_BOX_DIALOG, "");
        end
    end;

    --提供给hover到闪烁图标时弹出窗口中“忽略全部”按钮功能
    --http://tapd.oa.com/TGC/prong/stories/view/1010041181057203707
    ingore_all_flash_except_preload = function()
        tray_manager_model.remove_tray_status("talk");
        tray_manager_model.remove_tray_status("notify");

        tray_manager_model.remove_all_have_unread_msg_friend();
        tray_manager_model.remove_all_have_unread_msg_group();

        base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):restore_normal_tray_icon();
        tray_manager_control.switch_status(TRAY_STATUS.TRAY_SYSTEM_NORMAL);
        tray_manager_control.show_preview_tray_icon();
    end;

    open_all_talk_session = function()
        tray_manager_model.remove_tray_status("talk");
        tray_manager_model.remove_all_have_unread_msg_friend();
        tray_manager_model.remove_all_have_unread_msg_group();

        base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):restore_normal_tray_icon();
        tray_manager_control.switch_status(TRAY_STATUS.TRAY_SYSTEM_NORMAL);
        tray_manager_control.show_preview_tray_icon();

        -- 打开所有未读消息窗口
        if pwg_social_control:is_new_social_enabled() then
            comm_center.call_service("i_service_new_social_open_panel", { session_type = 1 });
        else
            local unread_friend_list = friend_list_control.get_all_unread_friend_id_list();
            local unread_group_list = group_list_control.get_all_unread_group_id_list();
            local table_param = {};
            if #unread_friend_list > 0 then
                table_param["friends"] = unread_friend_list;
            else
                base_utility.log("[tray_manager_control] unread_friend_list count is 0", true);
            end
            if #unread_group_list > 0 then
                table_param["groups"] = unread_group_list;
            else
                base_utility.log("[tray_manager_control] unread_group_list count is 0", true);
            end
            event_center.send_event(group_event.SHOW_TALK_DIALOG,
                                    base_utility.table_to_json(table_param));
        end
    end;

    -- 监听屏取消群闪烁消息
    on_mask_group_message = function(event_id, json_str)
        local p_tbl = base_utility.json_to_table(json_str);
        base_utility.log("[tray_manager_control] [on_mask_group_message] result:"..p_tbl["result"]..", group_id:"..p_tbl["group_id"]);
        if p_tbl["result"] == 0 and group_list_model.get_group_mask_msg_flag(p_tbl["group_id"]) == tray_manager_control.FORBID_MSG then
            --屏蔽闪烁成功，如果这个时候这个群之前触发了闪烁，则要取消闪烁
            --模拟一次读取消息了
            tray_manager_control.clear_group_msg(group_event.GROUP_CLEAR_MESSAGE, base_utility.table_to_json({group_id = p_tbl["group_id"]}));
        end
    end;

    -- 监听群退出、删除、解散操作
    recv_group_report = function(event_id, json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil then
            local report_type = param_tbl["type"];
            --类型定义参考message_box\\control.lua注释
            if report_type == 0x04 or report_type == 0x05 or report_type == 0x08 or report_type == 0x0b then
                local group_id = param_tbl["group_id"];
                base_utility.log("[tray_manager_control][recv_group_report] group is delete, type:"..tostring(report_type)..",group_id:"..tostring(group_id), true);
                --模拟一次读取消息了
                tray_manager_control.clear_group_msg(group_event.GROUP_CLEAR_MESSAGE, base_utility.table_to_json({group_id = group_id}));
            end
        end
    end;

    -- 监听删除好友操作
    recv_friend_report = function(event_id, json_str)
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil then
            --类型定义参考message_box\\control.lua注释
            local report_type = param_tbl["nType"];
            if report_type == 0x02 then
                local friend_id = param_tbl["sender"];
                base_utility.log("[tray_manager_control][recv_friend_report] friend is delete, friend_id:"..tostring(friend_id), true);
                --模拟一次读取消息了
                tray_manager_control.clear_friend_msg(group_event.FRIEND_CLEAR_MESSAGE, base_utility.table_to_json({friend_id = friend_id}));
            end
        end
    end;

    -- 主动删除好友
    on_delete_friend = function(event_id, json_str)        
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl ~= nil then
            base_utility.log("[tray_manager_control][on_delete_friend] delete op result:"..tostring(json_str), true);
            if param_tbl["result"] == 0 then
                local friend_id = param_tbl["friend_id"];
                --模拟一次读取消息了
                tray_manager_control.clear_friend_msg(group_event.FRIEND_CLEAR_MESSAGE, base_utility.table_to_json({friend_id = friend_id})); 
            end
        end
    end;
};

tray_manager_control.init();